﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.Forms;
using ERP_RORZE_PERMISSION.Base;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_ORDER.OrderObject.UserControls
{
    public partial class ucProductGroup : DevExpress.XtraEditors.XtraUserControl
    {
        private int index = 0;
        public int _action = 1;
        DXPopupMenu menu;
        ProductGroupHelper helper = new ProductGroupHelper(new ProductGroup());
        private LogUtils log;

        #region Handle methods
        public ucProductGroup()
        {
            InitializeComponent();
            InitPopupMenu();
            log = new LogUtils();

            BindDatatoGrid();
            gridView1.BestFitColumns();
            this.dropbtExport.DropDownControl = menu;
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            DeleteRows();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            EditRow();
        }

        private void btAddNew_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            BindDatatoGrid();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            var gridView = (GridView)sender;
            var point = gridView.GridControl.PointToClient(Control.MousePosition);

            DoDoubleClick(gridView, point);
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditRow();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {DeleteRows();

        }

        private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteRows();
            }
        }
        #endregion


        #region Private methods
        public void BindDatatoGrid()
        {
            ProductGroupHelper helper = new ProductGroupHelper(new ProductGroup());
            gridControl1.DataSource = helper.GetAll();
            gridView1.BestFitColumns();
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
        }

        private void DoDoubleClick(GridView v, Point p)
        {
            _action = 1;
            index = gridView1.FocusedRowHandle;
            var info = v.CalcHitInfo(p);
            if (info.InRow || info.InRowCell)
            {
                frm_ProductGroupEdit fr = new frm_ProductGroupEdit(this);
                if (!Utilities.FormUtil.IsFormOpen(fr))
                {
                    var row = new DataRow[gridView1.SelectedRowsCount];
                    row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

                    string id = row[0][OrderConstants.Entities.ProductGroup.FieldName.GroupID].ToString();
                    helper.ValueObject.GroupID = id;
                    helper.LoadOne();

                    fr.GroupID = helper.ValueObject.GroupID;
                    fr.GroupName = helper.ValueObject.GroupName;
                    fr.Note = helper.ValueObject.Note;

                    fr.StartPosition = FormStartPosition.CenterScreen;
                    fr.Show();
                }
            }
        }

        private void DeleteRows()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                var row = new DataRow[gridView1.SelectedRowsCount];
                var productId = new string[gridView1.SelectedRowsCount];

                for (int i = 0; i < gridView1.SelectedRowsCount; i++)
                {
                    row[i] = gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
                    productId[i] = row[i][OrderConstants.Entities.ProductGroup.FieldName.GroupID].ToString();
                }


                if (MessageBox.Show(OrderConstants.Message.DeleteQuestion, OrderConstants.Form.MessageBox.Warning,
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    ProductHelper helper = new ProductHelper(new Product());
                    for (int i = 0; i < productId.Length; i++)
                    {
                        helper.ValueObject.ProductID = productId[i];
                        if (helper.Delete() > 0)
                        {
                            //write Log
                            log.Table = LogConstant.Table.F2Order.ProductGroup;
                            log.Task = LogConstant.Task.Delete;
                            log.Description = "GroupID: " + row[i][OrderConstants.Entities.ProductGroup.FieldName.GroupID] + "; GroupName: " + row[i][OrderConstants.Entities.ProductGroup.FieldName.GroupName];
                            log.WriteLog();

                            _action = 2;
                        }
                        else
                        {
                            MessageBox.Show(OrderConstants.Message.DeleteFailed, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    BindDatatoGrid();


                }
            }
            else
            {
                MessageBox.Show(OrderConstants.Message.TableIsNull, OrderConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void EditRow()
        {

            if (gridView1.SelectedRowsCount > 0)
            {
                frm_ProductGroupEdit fr = new frm_ProductGroupEdit(this);
                if (!Utilities.FormUtil.IsFormOpen(fr))
                {


                    var row = new DataRow[gridView1.SelectedRowsCount];
                    row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

                    index = gridView1.FocusedRowHandle;
                    _action = 1;

                    string productId = row[0][OrderConstants.Entities.ProductGroup.FieldName.GroupID].ToString();
                    helper.ValueObject.GroupID = productId;
                    helper.LoadOne();

                    fr.GroupID = helper.ValueObject.GroupID;
                    fr.GroupName = helper.ValueObject.GroupName;
                    fr.Note = helper.ValueObject.Note;

                    fr.StartPosition = FormStartPosition.CenterScreen;
                    fr.Show();
                }
            }
            else
            {
                MessageBox.Show(OrderConstants.Message.TableIsNull, OrderConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void AddNew()
        {
            Cursor.Current = Cursors.WaitCursor;
            Form fr = new frm_ProductGroupAdd(this);
            fr.StartPosition = FormStartPosition.CenterScreen;

            if (!Utilities.FormUtil.IsFormOpen(fr))
            {
                fr.Show();
            }
            else
            {
                //fr.WindowState = FormWindowState.Normal;
                
                //fr.BringToFront();
                //fr.TopMost = true;
                //fr.Activate();
            }
            
            Cursor.Current = Cursors.Default;

        }


        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }

        #endregion


    }
}
